package cn.bingoo.nginx;

import static cn.bingoo.h2.domain.tables.Nginxstatus.NGINXSTATUS;

import java.awt.Color;
import java.awt.Dimension;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ListIterator;

import javax.swing.JPanel;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.time.Millisecond;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
import org.jooq.Result;
import org.jooq.impl.Factory;
import org.jooq.util.h2.H2Factory;

import cn.bingoo.h2.domain.tables.records.NginxstatusRecord;
import cn.bingoo.swing.utils.Dbs;

public class NginxStatusMultipleAxis extends ApplicationFrame
{
    private static final long serialVersionUID = 7591733667265273958L;

    public NginxStatusMultipleAxis(String paramString)
    {
        super(paramString);
        final XYDataset dataset = createDataset();
        ChartPanel localChartPanel = (ChartPanel) createDemoPanel(dataset);
        localChartPanel.setPreferredSize(new Dimension(600, 270));
        localChartPanel.setDomainZoomable(true);
        localChartPanel.setRangeZoomable(true);
        setContentPane(localChartPanel);
    }

    private static JFreeChart createChart(XYDataset dataset)
    {

        final JFreeChart chart = ChartFactory.createTimeSeriesChart(
                "Nginx Status Monitor",
                "Date", "Price Per Unit",
                dataset,
                true,
                true,
                false
                );
        chart.addSubtitle(new TextTitle("Nginx Status Monitor!"));
        chart.setBackgroundPaint(Color.white);

        //        final StandardLegend sl = (StandardLegend) chart.getLegend();
        //        sl.setDisplaySeriesShapes(true);

        final XYPlot plot = chart.getXYPlot();
        plot.setOrientation(PlotOrientation.VERTICAL);
        plot.setDomainPannable(true);
        plot.setRangePannable(true);
        plot.getRangeAxis().setFixedDimension(15.0D);

        plot.setBackgroundPaint(Color.lightGray);
        plot.setDomainGridlinePaint(Color.white);
        plot.setRangeGridlinePaint(Color.white);
        //        plot.setAxisOffset(new Spacer(Spacer.ABSOLUTE, 5.0, 5.0, 5.0, 5.0));
        plot.setDomainCrosshairVisible(true);
        plot.setRangeCrosshairVisible(true);
        //        final DateAxis axis = (DateAxis) plot.getDomainAxis();
        //        axis.setDateFormatOverride(new SimpleDateFormat("MMM-yyyy"));
        return chart;
    }

    public static Result<NginxstatusRecord> getNginxstatusRecord(String url) {
        Connection conn = null;
        try {
            conn = NginxDbUtils.getConnectionWithTableAutoCreate();
            Factory create = new H2Factory(conn);
            Result<NginxstatusRecord> lastRecord = create.selectFrom(NGINXSTATUS)
                    .where(NGINXSTATUS.URL.equal(url))
                    .orderBy(NGINXSTATUS.TS.asc()).fetch();
            return lastRecord != null ? lastRecord : null;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            Dbs.closeQuietly(conn);
        }
        return null;
    }

    private static XYDataset createDataset()
    {
        TimeSeriesCollection localTimeSeriesCollection = new TimeSeriesCollection();
        for (String url : NginxConfig.URLS) {
            TimeSeries localTimeSeries = new TimeSeries(url);
            ListIterator<NginxstatusRecord> listIterator = getNginxstatusRecord(url).listIterator();
            while (listIterator.hasNext()) {
                NginxstatusRecord next = listIterator.next();
                localTimeSeries.add(new Millisecond(next.getTs()), next.getActives());
            }
            localTimeSeriesCollection.addSeries(localTimeSeries);
        }
        return localTimeSeriesCollection;
    }

    public static JPanel createDemoPanel(XYDataset dataset)
    {
        JFreeChart localJFreeChart = createChart(dataset);
        ChartPanel localChartPanel = new ChartPanel(localJFreeChart);
        localChartPanel.setMouseWheelEnabled(true);
        return localChartPanel;
    }

    public static void main(String[] paramArrayOfString)
    {
        NginxStatusMultipleAxis localMultipleAxisDemo1 = new NginxStatusMultipleAxis(
                "JFreeChart: MultipleAxisDemo1.java");
        localMultipleAxisDemo1.pack();
        RefineryUtilities.centerFrameOnScreen(localMultipleAxisDemo1);
        localMultipleAxisDemo1.setVisible(true);
    }
}